package org.armstrong.datastructure;

class Node
{

    private Node leftNode;
    private Node rightNode;
    private int nodeValue;

    public Node(int nodeValue)
    {
        this.nodeValue = nodeValue;
    }

    public int getNodeValue()
    {
        return nodeValue;
    }

    public Node getLeftnode()
    {
        return leftNode;
    }

    public void setLeftnode(Node leftnode)
    {
        this.leftNode = leftnode;
    }

    public Node getRightnode()
    {
        return rightNode;
    }

    public void setRightnode(Node rightnode)
    {
        this.rightNode = rightnode;
    }

    void insert(int value)
    {
        if ( value > nodeValue )
        {
            insertRightSubNode(value);
        }
        else
        {
            insertLeftSubNode(value);
        }
    }

    private void insertRightSubNode(int value)
    {
        if ( rightNode == null )
        {
            rightNode = new Node(value);
        }
        else
        {
            rightNode.insert(value);
        }
    }

    private void insertLeftSubNode(int value)
    {
        if ( leftNode == null )
        {
            leftNode = new Node(value);
        }
        else
        {
            leftNode.insert(value);
        }
    }

    boolean contains(int nodeValue)
    {
        if ( this.nodeValue == nodeValue )
        {
            return true;
        }
        else
        {
            if ( nodeValue > this.nodeValue && rightNode != null )
            {
                return rightNode.contains(nodeValue);
            }
            else
            {
                if ( nodeValue < this.nodeValue && leftNode != null )
                {
                    return leftNode.contains(nodeValue);
                }
            }
        }
        return false;
    }
}
